package pro.shuangxi.model.wordList.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mybatisplus.pojo.WordList;
import mybatisplus.service.IWordListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.shuangxi.common.vo.Result;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * @author ：mengshx
 * @date ：Created in 2021/10/19 16:39
 * @description：单词Controller
 */
@RestController
@RequestMapping("wordlist")
public class WordListController {
    @Autowired
    IWordListService wordListService;
    @RequestMapping("everyday.do")
    public Result everyday(HttpServletRequest request) throws Exception{
        Random ramdom = new Random();
        int count = wordListService.count();
        int pageNum=ramdom.nextInt(count/10);
        int pageSize=10;
        List<WordList> list = wordListService.list(new QueryWrapper<WordList>().last(" limit " + pageNum + " , " + pageSize));
        return Result.OK(list);
        }
    @RequestMapping("randomOne.do")
    public Result randomOne(HttpServletRequest request) throws Exception{
//        Random ramdom = new Random();
//        int count = wordListService.count();
//        int index=ramdom.nextInt(count);
        WordList one = wordListService.getOne(new QueryWrapper<WordList>().orderByAsc("count").last(" limit " + 0 + " , " + 1));
        one.setCount(one.getCount() + 1);
        wordListService.updateById(one);
        return Result.OK(one);
        }
}
